package net.bsayiner.sadeceHaber.Activities;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Formatter;
import net.bsayiner.sadeceHaber.R;
import net.bsayiner.sadeceHaber.Utilities.News;
import net.bsayiner.sadeceHaber.Utilities.NewsType;
import net.bsayiner.sadeceHaber.Utilities.NewsXmlReader;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private SlidingHeaderAdapter slidingHeaderAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		startApplication();
	}

	private void startApplication() {
		requestWindowFeature(Window.FEATURE_NO_TITLE); // Ekran başlığını gizler
		setContentView(R.layout.main_layout); // Ekran üzerinde componentleri yerleştirir.
		initializeComponents();
		NewsReaderTask newsReaderTask = new NewsReaderTask();
		newsReaderTask.execute(String.valueOf(NewsType.NEWS.get(0)));
	}

	private void initializeComponents() {
		slidingHeaderAdapter = new SlidingHeaderAdapter(this);
		final HorizontialListView listview = (HorizontialListView) findViewById(R.id.listview);
		listview.setAdapter(slidingHeaderAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
				slidingHeaderAdapter.setSelectedItemPosition(arg2);
				slidingHeaderAdapter.notifyDataSetChanged();
				final int widthHeader = arg1.getWidth();
				final float numCountriesDisplayed = (float) listview.getWidth() / (float) widthHeader;
				listview.postDelayed(new Runnable() {
					public void run() {
						listview.scrollTo(Math.round(widthHeader * (arg2 - (numCountriesDisplayed - 1) / 2)));
					}
				}, 500);
				NewsReaderTask newsReaderTask = new NewsReaderTask();
				newsReaderTask.execute(String.valueOf(NewsType.NEWS.get(arg2)));
			}
		});

		ImageView refreshImage = (ImageView) findViewById(R.id.imgRefresh);
		refreshImage.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				NewsReaderTask newsReaderTask = new NewsReaderTask();
				newsReaderTask.execute(String.valueOf(NewsType.NEWS.get(slidingHeaderAdapter.getSelectedItemPosition())));
			}
		});
	}

	private class NewsReaderTask extends AsyncTask<String, Void, Void> {

		private NewsXmlReader newsXmlReader;
		private ArrayList<News> newsList;
		private ProgressDialog progressDialog;

		public NewsReaderTask() {
			newsXmlReader = NewsXmlReader.getInstance();
		}

		@Override
		protected void onPreExecute() {
			TextView textView = (TextView) findViewById(R.id.txtRefreshTimeStatus);
			textView.setText(new Formatter().format("%tT", Calendar.getInstance()).toString());
			progressDialog = ProgressDialog.show(MainActivity.this, "", "Haber bilgisi güncelleniyor...");
		}

		@Override
		protected Void doInBackground(String... params) {
			newsList = newsXmlReader.readFromXml(params[0]);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			ListView listView = (ListView) findViewById(R.id.list);
			NewsListAdapter newsListAdapter = new NewsListAdapter(MainActivity.this, R.layout.news_row_layout, newsList);
			listView.setAdapter(newsListAdapter);
			listView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
					Intent newsInfo = new Intent(MainActivity.this, NewsInfoActivity.class);
					newsInfo.putExtra("category", NewsType.NEWS_NAMES.get(slidingHeaderAdapter.getSelectedItemPosition()));
					newsInfo.putExtra("title", newsList.get(arg2).getTitle());
					newsInfo.putExtra("pubDate", newsList.get(arg2).getPubDate());
					newsInfo.putExtra("imageURL", newsList.get(arg2).getImg());
					newsInfo.putExtra("description", newsList.get(arg2).getDescription());
					newsInfo.putExtra("source", newsList.get(arg2).getAuthor());
					newsInfo.putExtra("newsLink", newsList.get(arg2).getLink());
					startActivity(newsInfo);
				}
			});
			progressDialog.dismiss();
		}
	}

}